/*
 *  main.cpp
 *  SDLtut1
 *
 *  Created by Darryl DeWeese on 1/12/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */


#include "SDL.h"
#include "SDL_image/SDL_image.h"
#include <string>
#include <stdlib.h>
#include "Enigma.h"

using namespace std;

#ifdef _WIN32
	const string resourcePath = "C:\\Users\\ryan\\Documents\\C++ Projects\\Enigma Physics Engine SVN\\Test code\\Resources\\";
#else
	const string resourcePath = "/Users/Darryl/Xcode Projects/Enigma/Test code/resources/";
#endif

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_DEPTH = 32;

const int NUM_STARS = 10000;

//this is a dummy function
int retardedFunction()
{
	return 5;
}

SDL_Surface* screen;
SDL_Surface* star;

SDL_Event event;
bool gameOn = true;

Entity* stars[NUM_STARS];
Singularity blackhole(500.0, 200.0, 1.0);//magnitude, radius, gravitational constant

SDL_Surface* sdlLoadImage(string fileName)
{
	SDL_Surface* image = IMG_Load(fileName.c_str());
	SDL_Surface* formattedImage = SDL_DisplayFormat(image);
	SDL_FreeSurface(image);
	Uint32 colorKey = SDL_MapRGB(formattedImage->format, 255, 0, 255);
	SDL_SetColorKey(formattedImage, SDL_SRCCOLORKEY, colorKey);
	return formattedImage;
}

void sdlBlit(int x, int y, SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	SDL_BlitSurface(source, clip, dest, &offset);
}

int main(int argc, char* argv[])
{
	int i;
	bool mouseIsDown = false;	
	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
	SDL_WM_SetCaption("Enigma Physics Engine Test", NULL);
	SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0,0,0));
	
	srand(2040375364);
	for (i = 0; i < NUM_STARS; i++)
		stars[i] = new Entity(Vec2d((float)(rand() % SCREEN_WIDTH), (float)(rand() % SCREEN_HEIGHT)));
	
	star = sdlLoadImage(resourcePath + "star.png");

	for (i = 0; i < NUM_STARS; i++)
		sdlBlit(stars[i]->pos.x, stars[i]->pos.y, star, screen, NULL);
	SDL_Flip(screen);
	
	while (gameOn)
	{
		while (SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				//MOUSE EVENT OF DEATH
				case SDL_MOUSEMOTION:
					if (mouseIsDown)
					{
						blackhole.pos.x = event.motion.x;
						blackhole.pos.y = event.motion.y;
					}
					break;
				case SDL_MOUSEBUTTONDOWN:
					mouseIsDown = true;
					blackhole.pos.x = event.motion.x;
					blackhole.pos.y = event.motion.y;
					blackhole.activate();
					break;
				case SDL_MOUSEBUTTONUP:
					mouseIsDown = false;
					for (i = 0; i < NUM_STARS; i++)
					{
						//stars[i]->vel.x = 0.0;
						//stars[i]->vel.y = 0.0;
					}
					blackhole.deactivate();
					break;
				case SDL_QUIT:
					gameOn = false;
					break;
			}
		}
		
		if (blackhole.isActive())
		{
			for (i = 0; i < NUM_STARS; i++)
				blackhole.affect(stars[i]);
		}
		
		for (i = 0; i < NUM_STARS; i++)
		{
			if ((stars[i]->pos.x < 0 && stars[i]->vel.x < 0) || (stars[i]->pos.x > SCREEN_WIDTH && stars[i]->vel.x > 0))
			{
				stars[i]->vel.x = -stars[i]->vel.x;
			}
			if ((stars[i]->pos.y < 0 && stars[i]->vel.y < 0) || (stars[i]->pos.y > SCREEN_HEIGHT && stars[i]->vel.y > 0))
			{
				stars[i]->vel.y = -stars[i]->vel.y;
			}
			stars[i]->vel.x *= 0.99;
			stars[i]->vel.y *= 0.99;
			stars[i]->update();
			stars[i]->accel.x = 0.0;
			stars[i]->accel.y = 0.0;
		}

		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,0,0,0));

		for (i = 0; i < NUM_STARS; i++)
			sdlBlit(stars[i]->pos.x, stars[i]->pos.y, star, screen, NULL);

		SDL_Flip(screen);
	}
	
	
	SDL_FreeSurface(star);
	SDL_Quit();
	return 0;
}






